package com.jiantong.web.open.app.work;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.service.BreedFactoryReadService;
import com.jiantong.work.model.PurchaseRecord;
import com.jiantong.work.service.*;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.Map;

/**
 * 采购中心-采购记录信息表
 *
 */
@Slf4j
@OpenBean
public class PurchaseRecordApis {
    @RpcConsumer
    private PurchaseRecordReadService purchaseRecordReadService;

    @RpcConsumer
    private PurchaseRecordWriteService purchaseRecordWriteService;

    @RpcConsumer
    private BreedFactoryReadService breedFactoryReadService;


    @OpenMethod(key = "find.purchase.record.by.id", paramNames = {"id"}, httpMethods = RequestMethod.GET)
    public PurchaseRecord findById(Long id){
        Response<PurchaseRecord> resp = purchaseRecordReadService.findById(id);
        if (!resp.isSuccess()){
            log.error("failed to find purchase record  by id = {}, cause : {}", id, resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }


    @OpenMethod(
            key = "paging.purchase.record",
            paramNames = {"pageNo", "pageSize",},
            httpMethods = RequestMethod.GET)
    public Paging<PurchaseRecord> pagingPurchaseRecord(Integer pageNo, Integer pageSize){
        Response<Paging<PurchaseRecord>> resp = purchaseRecordReadService.pagingPurchaseRecord(pageNo, pageSize);
        if (!resp.isSuccess()){
            log.error("failed to paging purchase record{}, cause : {}", resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }


    @OpenMethod(
            key = "paging.status.purchase.record",
            paramNames = {"pageNo", "pageSize","status","factoryId","fromDate","fromDate"},
            httpMethods = RequestMethod.GET)
    public Paging<PurchaseRecord> pagingRecord(Integer pageNo, Integer pageSize, Integer status,Long factoryId, Date fromDate, Date toDate){
        Response<Paging<PurchaseRecord>> resp = purchaseRecordReadService.pagingRecord(pageNo,pageSize,status,factoryId,fromDate,toDate);
        if (!resp.isSuccess()){
            log.error("failed to paging purchase.record{}, cause : {}", resp.getError());
            throw new OPServerException(resp.getError());
        }
        return resp.getResult();
    }
    @OpenMethod(
            key="create purchase record",
            paramNames = {"purchaseRecord,toStatus"},
            httpMethods = RequestMethod.POST)
    public Map<String, Object> create(PurchaseRecord purchaseRecord, Integer toStatus){
        LoginUser loginUser = UserUtil.getCurrentUser();
        BreedFactory breedFactory = breedFactoryReadService.findById(loginUser.getOrganizationId()).getResult();
        if(breedFactory.getIsScale() != 1){
            log.error("this is san hu");
            throw new OPClientException("this is san hu");
        }
        Response<Map<String , Object>> response =purchaseRecordWriteService.createPurchaseRecord(purchaseRecord,toStatus);
        if (!response.isSuccess()){
            log.error("failed to create purchase.record{}, cause : {}", response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    @OpenMethod(
            key="update purchase record",
            paramNames = {"purchaseRecord,toStatus"},
            httpMethods = RequestMethod.PUT)
    public Map<String,Object>update(PurchaseRecord purchaseRecord, Integer toStatus){
        if(purchaseRecord.getId() == null){
            log.error("failed to update quarantineCenterHarmless error = {}, cause : {}", purchaseRecord,purchaseRecord.getId());
            throw new OPClientException("quarantineCenterHarmless.id.is.null");
        }

        Response<Map<String , Object>> statusResponse = purchaseRecordReadService.findStatusById(purchaseRecord.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(purchaseRecord.getStatus())){
            log.error("quarantineCenterHarmless is already submitted error = {}, cause : {}", purchaseRecord.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException("this.report.is.already.submitted");
        }

        Response<Map<String,Object>>response=purchaseRecordWriteService.createPurchaseRecord(purchaseRecord,toStatus);
        if (!response.isSuccess()){
            log.error("failed to update purchase.record{}, cause : {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}